package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.UserInfoBean;
import com.example.wanbaqian.bean.WithdrawalsBean;
import com.example.wanbaqian.utils.BaseUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.SPUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 借款确认
 */
public class WithdrawalsConfirmActivity extends BaseActivity implements HttpCallback {

    private static final String TAG = "WithdrawalsConfirmActivity";
    @BindView(R.id.WithdrawalsConfirm_money)
    TextView WithdrawalsConfirmMoney;
    @BindView(R.id.WithdrawalsConfirm_time)
    TextView WithdrawalsConfirmTime;
    @BindView(R.id.WithdrawalsConfirm_type)
    TextView WithdrawalsConfirmType;
    @BindView(R.id.WithdrawalsConfirm_rate)
    TextView WithdrawalsConfirmRate;
    @BindView(R.id.WithdrawalsConfirm_bank)
    TextView WithdrawalsConfirmBank;
    @BindView(R.id.WithdrawalsConfirm_bankCard)
    TextView WithdrawalsConfirmBankCard;
    @BindView(R.id.WithdrawalsConfirm_ht)
    TextView WithdrawalsConfirmHt;
    @BindView(R.id.WithdrawalsConfirm_free)
    TextView WithdrawalsConfirm_free;


    @BindView(R.id.WithdrawlsCharge)
    TextView WithdrawlsCharges;
    @BindView(R.id.WithdrawalsConfirm_trueMoney)
    TextView WithdrawalsConfirm_trueMoney;
    @BindView(R.id.WithdrawalsConfirm_ok)
    Button WithdrawalsConfirmOk;
    private WithdrawalsBean bean;
    private UserInfoBean bean_user;

    @Override
    protected int getLayout() {
        return R.layout.activity_withdrawals_confirm;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.colorBackground);
        setToolBar("借款");
        bean_user=SPUtils.getUserInfo(mContext);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        String xieyi = "本人同意接受《快速签署协议》";
        WithdrawalsConfirmHt.setText(BaseUtils.setTextColor(mContext, xieyi, xieyi.indexOf("《"), xieyi.length(), R.color.colorBackground));
    }

    @Override
    protected void initDate() {
        bean = (WithdrawalsBean) getIntent().getExtras().getSerializable("bean");

        String serviceString = (String) SPUtils.get(this, "serviceString", "");
        Log.e(TAG, "serviceString: "+serviceString);
        String amountString = (String) SPUtils.get(this, "amountString", "");
        Log.e(TAG, "amountString: "+amountString);

        String dayString = (String) SPUtils.get(this, "dayString", "");
        Log.e(TAG, "dayString: "+dayString);

        String lixiString = (String) SPUtils.get(this, "lixiString", "");
        Log.e(TAG, "lixiString: "+lixiString);
        String realAmountString = (String) SPUtils.get(this, "realAmountString", "");
        Log.e(TAG, "lixiString: "+lixiString);

        WithdrawalsConfirmMoney.setText(amountString + "元");//借款金额
        WithdrawalsConfirmTime.setText(dayString + "天");//借款天书
        WithdrawalsConfirmType.setText("等额本息");
        WithdrawalsConfirmRate.setText(serviceString + "");//服务费
        WithdrawalsConfirmBank.setText(bean.getBank_name());
        WithdrawalsConfirmBankCard.setText(bean.getBank_account());
        WithdrawalsConfirm_free.setText(lixiString+"");//新增利息
        WithdrawalsConfirm_trueMoney.setText(""+realAmountString);//实际到账
        /*WithdrawalsConfirmMoney.setText(bean.getMoney() + "元");
        WithdrawalsConfirmTime.setText(bean.getTime() + "天");
        WithdrawalsConfirmType.setText("等额本息");
        WithdrawalsConfirmRate.setText(bean.getInterest_rate() + "%/天");
        WithdrawalsConfirmBank.setText(bean.getBank_name());
        WithdrawalsConfirmBankCard.setText(bean.getBank_account());
        WithdrawalsConfirm_trueMoney.setText(Float.parseFloat(bean.getMoney())-(Float.parseFloat(bean.getMoney()) * Float.parseFloat(bean.getInterest_rate()) * Float.parseFloat(bean.getTime())/100) + "元");
        */
    }

    @OnClick({R.id.WithdrawalsConfirm_ht, R.id.WithdrawalsConfirm_ok,R.id.WithdrawlsCharge,R.id.WithdrawalsConfirm_jkxy,R.id.WithdrawlsCharge_wtkk})
    public void onClick(View view) {
        String urls = "";
        switch (view.getId()) {

            case R.id.WithdrawalsConfirm_jkxy://借款协议

                //2017年9月15日四个协议存储本地

                String service_agree = (String) SPUtils.get(this, "getAgreement", "");

                urls=""+CommonValue.FAST_AGREE+service_agree+CommonValue.END_URL_SERVICE;
                Bundle bundle = new Bundle();
                bundle.putString("title", "借款协议");
                Log.e(TAG, "协议点击了onClick: "+urls);
                bundle.putString("url", urls);
                openActivity(PDFActivity.class, bundle);

                break;
            case R.id.WithdrawlsCharge_wtkk://委托扣款协议
                String hold_agreement = (String) SPUtils.get(this, "getAgreement", "");


                urls=""+CommonValue.FAST_AGREE+hold_agreement+CommonValue.END_URL_HOLD;
                Bundle bundle1 = new Bundle();
                bundle1.putString("title", "委托扣款协议");
                Log.e(TAG, "协议点击了onClick: "+urls);
                bundle1.putString("url", urls);
                openActivity(PDFActivity.class, bundle1);
                break;
            case R.id.WithdrawalsConfirm_ht://快速签署
                /*HashMap<String, String> params = new HashMap<>();
                params.put("id_name",bean.getId_name());
                params.put("id_no", DesUtils.decrypt(bean.getId_no(),SPUtils.getDesKey(mContext)));
                params.put("bank_id_name",bean.getId_name());
                params.put("bank_account",bean.getBank_account());
                params.put("bank_name",bean.getBank_name());
                params.put("loan_small",bean.getMoney());
                params.put("interest_rate",bean.getInterest_rate());
                params.put("repay_small",bean.getMoney());
                params.put("borrow_time",bean.getTime());
                JSONObject jsonObject=new JSONObject(params);
                String Endcode= StringUtils.encode(jsonObject.toString());
                String End_lend=StringUtils.encode(Endcode);
                String urls="http://wbq.yirongw.com/wbq/loan_agreement.html?data="+End_lend;*/


                String quick_sign = (String) SPUtils.get(this, "getAgreement", "");


                urls=""+CommonValue.FAST_AGREE+quick_sign+CommonValue.END_URL_QUICK;
                Bundle bundle2 = new Bundle();
                bundle2.putString("title", "快速签署协议");
                Log.e(TAG, "协议点击了onClick: "+urls);
                bundle2.putString("url", urls);
                openActivity(PDFActivity.class, bundle2);
                break;
            case R.id.WithdrawlsCharge://服务协议
                /*HashMap<String, String> params1 = new HashMap<>();
                params1.put("name",bean.getId_name());
                params1.put("id_no", DesUtils.decrypt(bean.getId_no(),SPUtils.getDesKey(mContext)));
                params1.put("account_name",bean.getId_name());
                params1.put("card_name",bean.getBank_name());
                params1.put("card_num",bean.getBank_account());
                params1.put("phone",bean_user.getLoan_mobile());
                JSONObject jsonObject1=new JSONObject(params1);
                String Endcode1= StringUtils.encode(jsonObject1.toString());
                String End_charge=StringUtils.encode(Endcode1);
                String url_re="http://wbq.yirongw.com/wbq/deductions_agreement.html?data="+End_charge;*/


                String manage_agree = (String) SPUtils.get(this, "getAgreement", "");


                urls=""+CommonValue.FAST_AGREE+manage_agree+CommonValue.END_URL_MANAGE;
                Bundle bundle3 = new Bundle();
                bundle3.putString("title", "服务协议");
                Log.e(TAG, "协议点击了onClick: "+urls);
                bundle3.putString("url", ""+urls);
                openActivity(PDFActivity.class, bundle3);

                break;
            case R.id.WithdrawalsConfirm_ok:
                HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.LOAN_APPLY, mContext,
                        new String[]{"accessid", "loan_money", "loan_days"},
                        new String[]{SPUtils.getAccessid(mContext), bean.getMoney(), bean.getTime()}), mContext, null, CommonValue.REQUEST_CODE1, this);
                break;
        }
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("bean", bean);
        openActivity(WithdrawalsProgressActivity.class, bundle);
        finish();
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }
}
